package it.polimi.digitalstate.manager;

import java.sql.Date;
import java.util.LinkedList;
import java.util.List;

import it.polimi.digitalstate.dto.CitizenDTO;
import it.polimi.digitalstate.dto.MembershipDTO;
import it.polimi.digitalstate.dto.PartyDTO;
import it.polimi.digitalstate.entities.*;
import it.polimi.digitalstate.persist.SuperEntityManager;
import it.polimi.digitalstate.persist.SuperEntityManagerException;
import it.polimi.digitalstate.persist.UniquePartyNameException;

import javax.annotation.security.RolesAllowed;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


/**
 * Session Bean implementation class PartyManager
 */
@LocalBean
@Stateless
public class PartyManager implements PartyManagerLocal {

    /**
     * Default constructor. 
     */
	@PersistenceContext
	private EntityManager em;
	
    public PartyManager() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public LinkedList<PartyDTO> getListOfParty() {
		Query q = em.createNamedQuery("getallparties");
		LinkedList<PartyDTO> cdto = new LinkedList<PartyDTO>() ;
		System.out.println("num parties: " + q.getResultList().toArray().length);
		for(int i=0;i<q.getResultList().toArray().length;i++){
			cdto.add(((Party)q.getResultList().toArray()[i]).getDTO());
		}
		return cdto;
	}

	@RolesAllowed({"citizen"})
	private Party createNewParty(PartyDTO party_dto) throws SuperEntityManagerException {
		Party p = new Party(party_dto);
		SuperEntityManager sem = new SuperEntityManager();
		sem.persist(em, p);
		Query q = em.createNamedQuery("party_getByName");
		q.setParameter("name", party_dto.getName());
		Object[] res = q.getResultList().toArray();
		if(res.length!=1){
			//TODO Throw Exception
		}
		return ((Party)res[0]);
	
	}

	@Override
	public MembershipDTO[] getListOfMember(PartyDTO party) {
		System.out.println("creation query getListOfMember: ");
		Query q = em.createNamedQuery("membership_getAllMemberFromParty");
		System.out.println("I DTOT ");
		//q.setParameter("party", (Party)DTOEntityTransformer.getEntity(em,party));
		Party p = (Party)DTOEntityTransformer.getEntity(em,party);
		System.out.println("F DTOT 1");
		q.setParameter("party", p);
		System.out.println("F DTOT 2");
		Object[] res = q.getResultList().toArray();
		MembershipDTO[] ret= new MembershipDTO[res.length];
		for(int i=0;i<res.length;i++){
			ret[i] = (((Membership)res[i]).getDTO());
		}
		System.out.println("Query getListOfMember: " + ret.length);
		return ret;
	}

	@Override
	public PartyDTO getPartyByCitizen(CitizenDTO citizen) {
		Query q = em.createNamedQuery("membership_getByCitizenID");
		q.setParameter("memberid", DTOEntityTransformer.getEntity(em,citizen));
		Object[] res = q.getResultList().toArray();
		if(res.length==1){
			return ((Membership)res[0]).getParty().getDTO();
			
		}
		return null;
	}

	@Override
	public boolean hasParty(CitizenDTO citizen) {
		return getPartyByCitizen(citizen)!=null;
	}

	@Override
	public PartyDTO getPartyByName(String name) {
		Query q = em.createNamedQuery("party_getByName");
		q.setParameter("name", name);
		Object[] res = q.getResultList().toArray();
		if(res.length!=1){
			//TODO Throw Exception
		}
		return ((Party)res[0]).getDTO();
	}

	@Override
	public MembershipDTO getMembershipByID(int id) {
		Query q = em.createNamedQuery("membership_getByID");
		q.setParameter("id", id);
		Object[] res = q.getResultList().toArray();
		if(res.length!=1){
			//TODO Throw Exception
		}
		return ((Membership)res[0]).getDTO();
	}
	@RolesAllowed({"citizen"})
	@Override
	public void createNewParty(PartyDTO party_dto, CitizenDTO citizen,String pdfcv) throws SuperEntityManagerException {
		if(PreconditionCheck.canCreateParty(citizen, em)){
			Party party = createNewParty(party_dto);
			MembershipDTO membership = new MembershipDTO(citizen, party_dto,new  Date(new java.util.Date().getTime()),pdfcv);
			Membership member=new Membership(party,(Citizen) DTOEntityTransformer.getEntity(em,citizen),membership);
			em.persist(member);
		}else{
			//TODO throw exception
		}
		
	}

	@Override
	public boolean canCreateParty(CitizenDTO citizen) {
		return PreconditionCheck.canRequestParty(citizen, em);
	}
	

}
